package graphics;

import env.Enviroment;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPanel;

/**
 *
 * @author orcas
 */
public class Board extends Applet {
    /*
     * serialVersionUID
     */

    private static final long serialVersionUID = 999999999999999999L;
    private static Frame frame = null;
    //private Enviroment env;
    //int rows, cols;
    // private static Boom bmain;
    private JPanel p;

    public Board(Enviroment env) {

        p = new JPanel();
        p.setLayout(new GridLayout(env.rows, env.cols));

        for (int i = 0; i < env.rows; i++) {
            for (int j = 0; j < env.cols; j++) {
                env.map[i][j].label.setToolTipText(i+", "+j);
                p.add(env.map[i][j].label);
            }
        }
        //setLayout(new GridLayout());

        add(p);
    }

    //TODO: make an arrow and not just a line
    public void drawArrow(int r1, int c1, int r2, int c2, Color color) {

        Graphics g = p.getGraphics();

        int x1 = c1 * 33 + 16;
        int x2 = c2 * 33 + 16;
        int y1 = r1 * 33 + 16;
        int y2 = r2 * 33 + 16;

        g.setColor(color);
        g.drawLine(x1, y1, x2, y2);
        System.err.println("LINE DRAWN!");

    }

    public static Board makeBoard(Enviroment env) {
        frame = new Frame("Board");
        Board bmain = new Board(env);
        frame.setSize(env.cols * 34, env.rows * 37);
        frame.setLocation(20, 20);
        frame.setAlwaysOnTop(true);
        //bmain.init(cols,rows);
        frame.add(bmain, BorderLayout.CENTER);
        bmain.start();
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                frame.dispose();
                System.exit(0);
                frame = null;
            }
        });

        //bmain.drawArrow(1, 2, 5, 15);
        return bmain;

    }
}
